3 * SpecialPage: handling special pages and lists thereof.
5 * To add a special page in an extension, add to $wgSpecialPages either
6 * an object instance or an array containing the name and constructor
7 * parameters. The latter is preferred for performance reasons.
9 * The object instantiated must be either an instance of SpecialPage or a
10 * sub-class thereof. It must have an execute() method, which sends the HTML
11 * for the special page to $wgOut. The parent class has an execute() method
12 * which distributes the call to the historical global functions. Additionally,
13 * execute() also checks if the user has the necessary access privileges
14 * and bails out if not.
16 * To add a core special page, use the similar static list in
17 * SpecialPage::$mList. To remove a core static special page at runtime, use
18 * a SpecialPage_initList hook.
21 * @ingroup SpecialPage
22 * @defgroup SpecialPage SpecialPage
26 * Factory for handling the special page list and generating SpecialPage objects
27 * @ingroup SpecialPage
30 class SpecialPageFactory
{
33 * List of special page names to the subclass of SpecialPage which handles them.
35 private static $mList = array(
36 // Maintenance Reports
37 'BrokenRedirects' => 'BrokenRedirectsPage',
38 'Deadendpages' => 'DeadendpagesPage',
39 'DoubleRedirects' => 'DoubleRedirectsPage',
40 'Longpages' => 'LongpagesPage',
41 'Ancientpages' => 'AncientpagesPage',
42 'Lonelypages' => 'LonelypagesPage',
43 'Fewestrevisions' => 'FewestrevisionsPage',
44 'Withoutinterwiki' => 'WithoutinterwikiPage',
45 'Protectedpages' => 'SpecialProtectedpages',
46 'Protectedtitles' => 'SpecialProtectedtitles',
47 'Shortpages' => 'ShortpagesPage',
48 'Uncategorizedcategories' => 'UncategorizedcategoriesPage',
49 'Uncategorizedimages' => 'UncategorizedimagesPage',
50 'Uncategorizedpages' => 'UncategorizedpagesPage',
51 'Uncategorizedtemplates' => 'UncategorizedtemplatesPage',
52 'Unusedcategories' => 'UnusedcategoriesPage',
53 'Unusedimages' => 'UnusedimagesPage',
54 'Unusedtemplates' => 'UnusedtemplatesPage',
55 'Unwatchedpages' => 'UnwatchedpagesPage',
56 'Wantedcategories' => 'WantedcategoriesPage',
57 'Wantedfiles' => 'WantedfilesPage',
58 'Wantedpages' => 'WantedpagesPage',
59 'Wantedtemplates' => 'WantedtemplatesPage',
62 'Allpages' => 'SpecialAllpages',
63 'Prefixindex' => 'SpecialPrefixindex',
64 'Categories' => 'SpecialCategories',
65 'Disambiguations' => 'DisambiguationsPage',
66 'Listredirects' => 'ListredirectsPage',
68 // Login/create account
69 'Userlogin' => 'LoginForm',
70 'CreateAccount' => 'SpecialCreateAccount',
73 'Block' => 'SpecialBlock',
74 'Unblock' => 'SpecialUnblock',
75 'BlockList' => 'SpecialBlockList',
76 'ChangePassword' => 'SpecialChangePassword',
77 'PasswordReset' => 'SpecialPasswordReset',
78 'DeletedContributions' => 'DeletedContributionsPage',
79 'Preferences' => 'SpecialPreferences',
80 'Contributions' => 'SpecialContributions',
81 'Listgrouprights' => 'SpecialListGroupRights',
82 'Listusers' => 'SpecialListUsers' ,
83 'Listadmins' => 'SpecialListAdmins',
84 'Listbots' => 'SpecialListBots',
85 'Activeusers' => 'SpecialActiveUsers',
86 'Userrights' => 'UserrightsPage',
87 'EditWatchlist' => 'SpecialEditWatchlist',
89 // Recent changes and logs
90 'Newimages' => 'SpecialNewFiles',
91 'Log' => 'SpecialLog',
92 'Watchlist' => 'SpecialWatchlist',
93 'Newpages' => 'SpecialNewpages',
94 'Recentchanges' => 'SpecialRecentchanges',
95 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
96 'Tags' => 'SpecialTags',
98 // Media reports and uploads
99 'Listfiles' => 'SpecialListFiles',
100 'Filepath' => 'SpecialFilepath',
101 'MIMEsearch' => 'MIMEsearchPage',
102 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
103 'Upload' => 'SpecialUpload',
104 'UploadStash' => 'SpecialUploadStash',
106 // Wiki data and tools
107 'Statistics' => 'SpecialStatistics',
108 'Allmessages' => 'SpecialAllmessages',
109 'Version' => 'SpecialVersion',
110 'Lockdb' => 'SpecialLockdb',
111 'Unlockdb' => 'SpecialUnlockdb',
113 // Redirecting special pages
114 'LinkSearch' => 'LinkSearchPage',
115 'Randompage' => 'Randompage',
116 'Randomredirect' => 'SpecialRandomredirect',
119 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
120 'Mostimages' => 'MostimagesPage',
121 'Mostlinked' => 'MostlinkedPage',
122 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
123 'Mostcategories' => 'MostcategoriesPage',
124 'Mostrevisions' => 'MostrevisionsPage',
127 'ComparePages' => 'SpecialComparePages',
128 'Export' => 'SpecialExport',
129 'GlobalFileUsage' => 'SpecialGlobalFileUsage',
130 'GlobalTemplateUsage' => 'SpecialGlobalTemplateUsage',
131 'Import' => 'SpecialImport',
132 'Undelete' => 'SpecialUndelete',
133 'Whatlinkshere' => 'SpecialWhatlinkshere',
134 'MergeHistory' => 'SpecialMergeHistory',
137 'Booksources' => 'SpecialBookSources',
139 // Unlisted / redirects
140 'Blankpage' => 'SpecialBlankpage',
141 'Blockme' => 'SpecialBlockme',
142 'Emailuser' => 'SpecialEmailUser',
143 'Movepage' => 'MovePageForm',
144 'Mycontributions' => 'SpecialMycontributions',
145 'Mypage' => 'SpecialMypage',
146 'Mytalk' => 'SpecialMytalk',
147 'Myuploads' => 'SpecialMyuploads',
148 'PermanentLink' => 'SpecialPermanentLink',
149 'Revisiondelete' => 'SpecialRevisionDelete',
150 'Specialpages' => 'SpecialSpecialpages',
151 'Userlogout' => 'SpecialUserlogout',
154 private static $mAliases;
157 * Initialise the special page list
158 * This must be called before accessing SpecialPage::$mList
162 static function getList() {
163 global $wgSpecialPages;
164 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
165 global $wgEnableEmail;
167 if ( !is_object( self
::$mList ) ) {
168 wfProfileIn( __METHOD__
);
170 if ( !$wgDisableCounters ) {
171 self
::$mList['Popularpages'] = 'PopularpagesPage';
174 if ( !$wgDisableInternalSearch ) {
175 self
::$mList['Search'] = 'SpecialSearch';
178 if ( $wgEmailAuthentication ) {
179 self
::$mList['Confirmemail'] = 'EmailConfirmation';
180 self
::$mList['Invalidateemail'] = 'EmailInvalidation';
183 if ( $wgEnableEmail ) {
184 self
::$mList['ChangeEmail'] = 'SpecialChangeEmail';
187 // Add extension special pages
188 self
::$mList = array_merge( self
::$mList, $wgSpecialPages );
191 // This hook can be used to remove undesired built-in special pages
192 wfRunHooks( 'SpecialPage_initList', array( &self
::$mList ) );
194 // Cast to object: func()[$key] doesn't work, but func()->$key does
195 settype( self
::$mList, 'object' );
197 wfProfileOut( __METHOD__
);
203 * Initialise and return the list of special page aliases. Returns an object with
204 * properties which can be accessed $obj->pagename - each property is an array of
205 * aliases; the first in the array is the cannonical alias. All registered special
206 * pages are guaranteed to have a property entry, and for that property array to
207 * contain at least one entry (English fallbacks will be added if necessary).
210 static function getAliasList() {
211 if ( !is_object( self
::$mAliases ) ) {
213 $aliases = $wgContLang->getSpecialPageAliases();
215 // Objects are passed by reference by default, need to create a copy
216 $missingPages = clone self
::getList();
218 self
::$mAliases = array();
219 foreach ( $aliases as $realName => $aliasList ) {
220 foreach ( $aliasList as $alias ) {
221 self
::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
223 unset( $missingPages->$realName );
225 foreach ( $missingPages as $name => $stuff ) {
226 self
::$mAliases[$wgContLang->caseFold( $name )] = $name;
229 // Cast to object: func()[$key] doesn't work, but func()->$key does
230 self
::$mAliases = (object)self
::$mAliases;
232 return self
::$mAliases;
236 * Given a special page name with a possible subpage, return an array
237 * where the first element is the special page name and the second is the
240 * @param $alias String
241 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
243 public static function resolveAlias( $alias ) {
245 $bits = explode( '/', $alias, 2 );
247 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
248 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
249 if ( isset( self
::getAliasList()->$caseFoldedAlias ) ) {
250 $name = self
::getAliasList()->$caseFoldedAlias;
252 return array( null, null );
255 if ( !isset( $bits[1] ) ) { // bug 2087
261 return array( $name, $par );
265 * Add a page to a certain display group for Special:SpecialPages
267 * @param $page Mixed: SpecialPage or string
268 * @param $group String
270 public static function setGroup( $page, $group ) {
271 global $wgSpecialPageGroups;
272 $name = is_object( $page ) ?
$page->getName() : $page;
273 $wgSpecialPageGroups[$name] = $group;
277 * Get the group that the special page belongs in on Special:SpecialPage
279 * @param $page SpecialPage
281 public static function getGroup( &$page ) {
282 $name = $page->getName();
284 global $wgSpecialPageGroups;
285 static $specialPageGroupsCache = array();
286 if ( isset( $specialPageGroupsCache[$name] ) ) {
287 return $specialPageGroupsCache[$name];
289 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $name ) );
290 if ( !$msg->isBlank() ) {
291 $group = $msg->text();
293 $group = isset( $wgSpecialPageGroups[$name] )
294 ?
$wgSpecialPageGroups[$name]
297 if ( $group == '-' ) {
300 $specialPageGroupsCache[$name] = $group;
305 * Check if a given name exist as a special page or as a special page alias
307 * @param $name String: name of a special page
308 * @return Boolean: true if a special page exists with this name
310 public static function exists( $name ) {
311 list( $title, /*...*/ ) = self
::resolveAlias( $name );
312 return property_exists( self
::getList(), $title );
316 * Find the object with a given name and return it (or NULL)
318 * @param $name String Special page name, may be localised and/or an alias
319 * @return SpecialPage object or null if the page doesn't exist
321 public static function getPage( $name ) {
322 list( $realName, /*...*/ ) = self
::resolveAlias( $name );
323 if ( property_exists( self
::getList(), $realName ) ) {
324 $rec = self
::getList()->$realName;
325 if ( is_string( $rec ) ) {
327 return new $className;
328 } elseif ( is_array( $rec ) ) {
329 // @deprecated, officially since 1.18, unofficially since forever
330 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
331 $className = array_shift( $rec );
332 self
::getList()->$realName = MWFunction
::newObj( $className, $rec );
334 return self
::getList()->$realName;
341 * Return categorised listable special pages which are available
342 * for the current user, and everyone.
344 * @return Array( String => Specialpage )
346 public static function getUsablePages() {
349 foreach ( self
::getList() as $name => $rec ) {
350 $page = self
::getPage( $name );
351 if ( $page->isListed()
353 !$page->isRestricted()
354 ||
$page->userCanExecute( $wgUser )
357 $pages[$name] = $page;
364 * Return categorised listable special pages for all users
366 * @return Array( String => Specialpage )
368 public static function getRegularPages() {
370 foreach ( self
::getList() as $name => $rec ) {
371 $page = self
::getPage( $name );
372 if ( $page->isListed() && !$page->isRestricted() ) {
373 $pages[$name] = $page;
380 * Return categorised listable special pages which are available
381 * for the current user, but not for everyone
383 * @return Array( String => Specialpage )
385 public static function getRestrictedPages() {
388 foreach ( self
::getList() as $name => $rec ) {
389 $page = self
::getPage( $name );
392 && $page->isRestricted()
393 && $page->userCanExecute( $wgUser )
395 $pages[$name] = $page;
402 * Execute a special page path.
403 * The path may contain parameters, e.g. Special:Name/Params
404 * Extracts the special page name and call the execute method, passing the parameters
406 * Returns a title object if the page is redirected, false if there was no such special
407 * page, and true if it was successful.
409 * @param $title Title object
410 * @param $context RequestContext
411 * @param $including Bool output is being captured for use in {{special:whatever}}
415 public static function executePath( Title
&$title, RequestContext
&$context, $including = false ) {
416 wfProfileIn( __METHOD__
);
418 // @todo FIXME: Redirects broken due to this call
419 $bits = explode( '/', $title->getDBkey(), 2 );
421 if ( !isset( $bits[1] ) ) { // bug 2087
426 $page = self
::getPage( $name );
429 $context->getOutput()->setArticleRelated( false );
430 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
431 $context->getOutput()->setStatusCode( 404 );
432 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
433 wfProfileOut( __METHOD__
);
437 // Page exists, set the context
438 $page->setContext( $context );
441 // Redirect to canonical alias for GET commands
442 // Not for POST, we'd lose the post data, so it's best to just distribute
443 // the request. Such POST requests are possible for old extensions that
444 // generate self-links without being aware that their default name has
446 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
447 $query = $context->getRequest()->getQueryValues();
448 unset( $query['title'] );
449 $query = wfArrayToCGI( $query );
450 $title = $page->getTitle( $par );
451 $url = $title->getFullUrl( $query );
452 $context->getOutput()->redirect( $url );
453 wfProfileOut( __METHOD__
);
456 $context->setTitle( $page->getTitle() );
459 } elseif ( !$page->isIncludable() ) {
460 wfProfileOut( __METHOD__
);
464 $page->including( $including );
466 // Execute special page
467 $profName = 'Special:' . $page->getName();
468 wfProfileIn( $profName );
469 $page->execute( $par );
470 wfProfileOut( $profName );
471 wfProfileOut( __METHOD__
);
476 * Just like executePath() except it returns the HTML instead of outputting it
477 * Returns false if there was no such special page, or a title object if it was
480 * Also saves the current $wgTitle, $wgOut, and $wgRequest variables so that
481 * the special page will get the context it'd expect on a normal request,
482 * and then restores them to their previous values after.
484 * @param $title Title
486 * @return String: HTML fragment
488 static function capturePath( &$title ) {
489 global $wgOut, $wgTitle, $wgRequest;
491 $oldTitle = $wgTitle;
493 $oldRequest = $wgRequest;
495 // Don't want special pages interpreting ?feed=atom parameters.
496 $wgRequest = new FauxRequest( array() );
498 $context = new RequestContext
;
499 $context->setTitle( $title );
500 $context->setRequest( $wgRequest );
501 $wgOut = $context->getOutput();
503 $ret = self
::executePath( $title, $context, true );
504 if ( $ret === true ) {
505 $ret = $wgOut->getHTML();
507 $wgTitle = $oldTitle;
509 $wgRequest = $oldRequest;
514 * Get the local name for a specified canonical name
516 * @param $name String
517 * @param $subpage String|Bool
521 static function getLocalNameFor( $name, $subpage = false ) {
523 $aliases = $wgContLang->getSpecialPageAliases();
525 if ( isset( $aliases[$name][0] ) ) {
526 $name = $aliases[$name][0];
528 // Try harder in case someone misspelled the correct casing
530 foreach ( $aliases as $n => $values ) {
531 if ( strcasecmp( $name, $n ) === 0 ) {
532 wfWarn( "Found alias defined for $n when searching for " .
533 "special page aliases for $name. Case mismatch?" );
540 wfWarn( "Did not find alias for special page '$name'. " .
541 "Perhaps no aliases are defined for it?" );
544 if ( $subpage !== false && !is_null( $subpage ) ) {
545 $name = "$name/$subpage";
547 return $wgContLang->ucfirst( $name );
551 * Get a title for a given alias
553 * @param $alias String
555 * @return Title or null if there is no such alias
557 static function getTitleForAlias( $alias ) {
558 $name = self
::resolveAlias( $alias );
560 return SpecialPage
::getTitleFor( $name );